package com.geek.spring.bucks;


import com.geek.spring.bucks.model.Coffee;
import com.geek.spring.bucks.service.CoffeeOrderService;
import com.geek.spring.bucks.service.CoffeeService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;

import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.*;

@Slf4j
@EnableTransactionManagement
@SpringBootApplication
@MapperScan("com.geek.spring.bucks.mapper")
@EnableRedisRepositories
public class SpringBucksApp implements ApplicationRunner {

    @Autowired
    private CoffeeService coffeeService;


    public static void main(String[] args) {
        SpringApplication.run(SpringBucksApp.class, args);
    }


//    @Bean
//    @ConfigurationProperties("redis")
//    public JedisPoolConfig jedisPoolConfig() {
//        return new JedisPoolConfig();
//    }
//    @Bean(destroyMethod = "close")
//    public JedisPool jedisPool(@Value("${redis.host}") String host) {
//        return new JedisPool(jedisPoolConfig(), host);
//    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
//        this.generateArtifacts();
//        insert();
//        update();
//        selectPage();
        getById();
    }

    private void generateArtifacts() throws Exception {
        List<String> warnings = new ArrayList<>();
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(
                this.getClass().getResourceAsStream("/generatorConfig.xml"));
        DefaultShellCallback callback = new DefaultShellCallback(true);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
    }

    public void getById(){
        coffeeService.findById(2L);
    }
    public void insert(){

        log.info("外部");
            log.info( coffeeService.selectCount().toString());
        Coffee coffee = new Coffee();
        coffee.setId(11L);
        coffee.setName("ztc");
        coffee.setUpdateTime(new Date());
        coffee.setPrice(Money.of(CurrencyUnit.of("CNY"), 35.00));
        try {
            coffeeService.insertCoffee(coffee);
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("外部");
        log.info(coffeeService.findById(10L).toString());
    }

    public void update(){
        log.info("修改前");
        log.info( coffeeService.findById(10L).toString());
        Coffee coffee = new Coffee();
        coffee.setName("ztc1");
        coffee.setId(10L);
        coffee.setUpdateTime(new Date());
        coffee.setPrice(Money.of(CurrencyUnit.of("CNY"), 37.00));
        try {
            coffeeService.updateCoffee(coffee);
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("修改後");
        log.info(coffeeService.selectCount().toString());
        log.info(coffeeService.findById(10L).toString());

    }

    public void selectPage(){
        Coffee coffee = new Coffee();

        PageInfo<Coffee> coffeePageInfo = coffeeService.selectPage(1, 3, coffee);
        System.out.println(coffeePageInfo.toString());
        List<Coffee> list = coffeePageInfo.getList();
        log.info("列表");
        list.forEach(l->{

            System.out.println(l.toString());
        });

    }

}
